/**
 * @UiResource.cpp
 * Copyright 2013 (C) FatalEror / Dean Bahari
 * 
 * UiPack is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * See LICENSE file in main folder for more information.
 */

#include "UiCommon.hpp"
#include "UiResource.hpp"

UiResource::UiResource()
{
	iSysTitle   = this->loadImage(L"IMG_SYS_TITLE");
	iSysNormal  = this->loadImage(L"IMG_SYS_NORMAL");
	iSysHover   = this->loadImage(L"IMG_SYS_HOVER");
	iSysClick   = this->loadImage(L"IMG_SYS_CLICK");
	iBtnNormal  = this->loadImage(L"IMG_BTN_NORMAL");
	iBtnHover   = this->loadImage(L"IMG_BTN_HOVER");
	iBtnClick   = this->loadImage(L"IMG_BTN_CLICK");
	iMBIHover   = this->loadImage(L"IMG_MENUITEM_HOVER");
	iMBIClick   = this->loadImage(L"IMG_MENUITEM_CLICK");
	iToolTitle  = this->loadImage(L"IMG_TOOL_TITLE");
	iToolClose  = this->loadImage(L"IMG_TOOL_CLOSE");
	iToolDetach = this->loadImage(L"IMG_TOOL_DETACH");
	bBackground = new Gdiplus::SolidBrush(Gdiplus::Color(130, 130, 130));
	bBorder     = new Gdiplus::SolidBrush(Gdiplus::Color(200, 200, 222));
	bText       = new Gdiplus::SolidBrush(Gdiplus::Color(2, 2, 2));
	bToolWin    = new Gdiplus::SolidBrush(Gdiplus::Color(237, 245, 248));
	bTooltip    = new Gdiplus::SolidBrush(Gdiplus::Color(232, 232, 242));
	bBlank      = new Gdiplus::SolidBrush(Gdiplus::Color(0, 0, 0));
	pBorder     = new Gdiplus::Pen(Gdiplus::Color(85, 85, 94), 1);
	pText       = new Gdiplus::Pen(Gdiplus::Color(64, 2, 2, 2), 1);
	fText       = new Gdiplus::FontFamily(L"Verdana");

	pText->SetLineJoin(Gdiplus::LineJoinRound);
}

UiResource::~UiResource()
{
	delete iSysTitle;
	delete iSysNormal;
	delete iSysHover;
	delete iSysClick;
	delete iBtnNormal;
	delete iBtnHover;
	delete iBtnClick;
	delete iMBIHover;
	delete iMBIClick;
	delete iToolTitle;
	delete iToolClose;
	delete iToolDetach;
	delete bBackground;
	delete bBorder;
	delete bText;
	delete bToolWin;
	delete bTooltip;
	delete bBlank;
	delete pBorder;
	delete pText;
	delete fText;
}

Gdiplus::Image *UiResource::getImage(int type)
{
	switch (type) {
	case UiResource::UI_IMAGE_SYS_TITLEBAR:
		return iSysTitle;
	case UiResource::UI_IMAGE_SYS_NORMAL:
		return iSysNormal;
	case UiResource::UI_IMAGE_SYS_HOVER:
		return iSysHover;
	case UiResource::UI_IMAGE_SYS_CLICK:
		return iSysClick;
	case UiResource::UI_IMAGE_BTN_NORMAL:
		return iBtnNormal;
	case UiResource::UI_IMAGE_BTN_HOVER:
		return iBtnHover;
	case UiResource::UI_IMAGE_BTN_CLICK:
		return iBtnClick;
	case UiResource::UI_IMAGE_MENUBARITEM_HOVER:
		return iMBIHover;
	case UiResource::UI_IMAGE_MENUBARITEM_CLICK:
		return iMBIClick;
	case UiResource::UI_IMAGE_TOOL_TITLE:
		return iToolTitle;
	case UiResource::UI_IMAGE_TOOL_CLOSE:
		return iToolClose;
	case UiResource::UI_IMAGE_TOOL_DETACH:
		return iToolDetach;
	default:
		break;
	}
	return NULL;
}

Gdiplus::Brush *UiResource::getBrush(int type)
{
	switch (type) {
	case UiResource::UI_BRUSH_BACKGROUND:
		return bBackground;
	case UiResource::UI_BRUSH_BORDER:
		return bBorder;
	case UiResource::UI_BRUSH_TEXT:
		return bText;
	case UiResource::UI_BRUSH_TOOLWINDOW:
		return bToolWin;
	case UiResource::UI_BRUSH_TOOLTIP:
		return bTooltip;
	case UiResource::UI_BRUSH_BLANK:
		return bBlank;
	default:
		break;
	}
	return NULL;
}

Gdiplus::Pen *UiResource::getPen(int type)
{
	switch (type) {
	case UiResource::UI_PEN_BORDER:
		return pBorder;
	case UiResource::UI_PEN_TEXT:
		return pText;
	default:
		break;
	}
	return NULL;
}

Gdiplus::FontFamily *UiResource::getFont()
{
	return fText;
}

Gdiplus::Image *UiResource::loadImage(const wchar_t *imgName)
{
	IStream *stream = NULL;

	HRSRC hResource = FindResource(0, imgName, RT_RCDATA);
	if (!hResource)
		return NULL;

	HGLOBAL hGlobal = LoadResource(0, hResource);
	if (!hGlobal)
		return NULL;

	void *resData = (char*)LockResource(hGlobal);
	int   resSize = SizeofResource(0, hResource);

	HGLOBAL hBuffer = GlobalAlloc(GMEM_MOVEABLE, resSize);
	if (!hBuffer)
		return NULL;

	void *pBuffer = GlobalLock(hBuffer);
	if (!pBuffer) {
		GlobalFree(hBuffer);
		return NULL;
	}

	CopyMemory(pBuffer, resData, resSize);
	CreateStreamOnHGlobal(hBuffer, false, &stream);

	Gdiplus::Image *imgTemp = new Gdiplus::Image(stream);
	stream->Release();

	GlobalUnlock(hBuffer);
	GlobalFree(hBuffer);

	return imgTemp;
}